package cn.tedu.javagame.star;

import java.awt.*;

public class Star {
    //属性
    int x,y;
    Color color;
    int speed;
    String label = "★";
    private float hue; // 色调 (0.0-1.0)
    private float saturation = 0.8f; // 饱和度
    private float brightness = 1.0f; // 亮度
    private float hueChangeRate = 0.01f + (float) (Math.random() * 0.02f);// 色调变化速率

    //构造
    public Star(){
        this.x = (int) (Math.random() * (StarFrame.WIDTH + 1));
        this.y = (int) (Math.random() * (StarFrame.HEIGHT + 1));//随机数
        this.color = new Color((int) (Math.random() * (256)),(int)( Math.random() * (256)),(int)( Math.random() * (256)));
        this.speed = (int) (Math.random() * 4 + 1);
    }
    //行为
    public void move(){
        this.y += this.speed;
        if(this.y > StarFrame.HEIGHT){
            this.y = 0;
            this.x = (int) (Math.random() * (StarFrame.WIDTH + 1));
//            this.color = new Color((int) (Math.random() * (256)),(int)( Math.random() * (256)),(int)( Math.random() * (256)));
        }
    }
    public void changeColor() {
        // 每调用5次才改变颜色，减缓变化速度
        if (Math.random() < 0.2) {
            // 增加色调值，实现颜色循环变化
            hue += hueChangeRate;
            if (hue > 1.0f) {
                hue -= 1.0f; // 保持在0.0-1.0范围内
            }

            // 使用HSB颜色模型生成渐变颜色
            this.color = Color.getHSBColor(hue, saturation, brightness);
//            this.color = new Color((int) (Math.random() * (256)),(int)( Math.random() * (256)),(int)( Math.random() * (256)));
        }
    }

}
